<?php

namespace App\Http\Controllers\Admin;

use App;
use App\Http\Controllers\Controller as BaseController;
use Elasticsearch\ClientBuilder;
use App\Helper\EsTrait;

class TestESController extends BaseController
{

    public function test()
    {
        $client = EsTrait::getClient();
        $index  = "test_db";                        # 数据库

        # 创建索引【数据库】
        if (!EsTrait::indexExists($client, $index)) {
            //            $client->indices()->delete(['index' => $index]);
            $params = [
                'index' => $index,
                "number_of_shards" => 2,   # 主分片 一旦设置不可修改 【副本 * 主分片 + 主分片 = 总分片】
                "number_of_replicas" => 2   # 副本分片 可修改【俩个副本表示 为每一个主分片 设置俩个副本，这里就会有 6个分片，因为还有主分片2个】
            ];

            $indexResponse = $client->indices()->create($params);
            var_dump($indexResponse);
        }


        # 创建mapping
        $params = [
            'index' => $index,
            'body'  => []
        ];

        $es             = new App\Helper\ESearch();
        $map            = $es::getMapping();
        $params["body"] = $map;
        $mapResponse    = $client->indices()->putMapping($params);
        var_dump('$mapResponse');
        var_dump($mapResponse);


        # 写入数据
        $params = [
            'index' => $index,
            //            'id'    => 1, # 一条时使用
            'body'  => [
                [
                    'index' => ['_id' => 6],
                ],
                [
                    'id'                => 6,
                    'yyid'              => "yyid_u8wrjsfudhgkjdfny47846375y3-2",
                    'pro'               => "pro 芜湖 天空下起了雨 这是我对你的思念",
                    'pro_yyid'          => "pro_yyid_u8wrjsfudhgkjdfny47846375y3-2",
                    'city'              => "芜湖市-2",
                    'city_yyid'         => "city_yyid_dskfusgj0egu8888-2",
                    'hospital_name'     => "医院名称-2",
                    'addr'              => "地址：芜湖宣武区人民南路100号-2",
                    'level'             => 4,
                    'old_hospital_name' => "老医院名称-2",
                    'old_alias_name'    => "芜湖别名-2",
                    'old_addr'          => "芜湖地址-2",
                    'status'            => 22
                ],
                [
                    'index' => ['_id' => 7],
                ],
                [
                    'id'                => 7,
                    'yyid'              => "yyid_u8wrjsfudhgkjdfny47846375y3-3",
                    'pro'               => "pro 芜湖 这个世界呢",
                    'pro_yyid'          => "pro_yyid_u8wrjsfudhgkjdfny47846375y3-3",
                    'city'              => "芜湖市-3",
                    'city_yyid'         => "city_yyid_dskfusgj0egu8888-3",
                    'hospital_name'     => "医院名称-3",
                    'addr'              => "地址：芜湖宣武区人民南路100号-3",
                    'level'             => 6,
                    'old_hospital_name' => "老医院名称-3",
                    'old_alias_name'    => "芜湖差一个别名-3",
                    'old_addr'          => "芜湖差一个地址-3",
                    'status'            => 6
                ]
            ]
        ];
        //        $createResponse = $client->index($params);  # 一条
        $createResponse = $client->bulk($params);   # 多条
        var_dump('===');
        var_dump($createResponse);

        # 返回：
        /**
         * array(8) {
         * ["_index"]=>
         * string(7) "test_db"
         * ["_type"]=>
         * string(4) "_doc"
         * ["_id"]=>
         * string(1) "1"
         * ["_version"]=>
         * int(1)
         * ["result"]=>
         * string(7) "created"
         * ["_shards"]=>
         * array(3) {
         * ["total"]=>
         * int(2)
         * ["successful"]=>
         * int(1)
         * ["failed"]=>
         * int(0)
         * }
         * ["_seq_no"]=>
         * int(0)
         * ["_primary_term"]=>
         * int(1)
         * }
         */

        # 多条返回：检查响应结果
//        if ($createResponse['errors']) {
//            foreach ($createResponse['items'] as  $item) {
//                if (isset($item['index']['error'])) {
//                    // 处理错误
//                }
//            }
//        } else {
//            // 批量写入成功
//        }
    }

    public function test2()
    {
        $params = [
            'index' => "aaa",
            'body'  => [
                'mappings' => [
                    'properties' => [
                        //之后可以进行搜索的字段
                        'room-name' => [
                            'type'            => 'text',
                            "analyzer"        => "ik_max_word",
                            "search_analyzer" => "ik_max_word"
                        ]
                    ]
                ]
            ]
        ];

        $id      = 12;
        $records = [
            "index" => "aaa",
            "type"  => "_doc",
            "id"    => $id, # id：该字段不写也问题不大，因为该id会自动生成，但是为了我们在执行删除数据的时候为我们自己提供便利，通常情况下将该id设为数据id
            "body"  => [
                "id"   => $id,
                "sex"  => 1,
                "name" => "我的名字",
                "desc" => "描述：阿斯顿就看和是非i好的乖不乖 ",
            ],
        ];
        var_dump("========");
        var_dump("========");
        var_dump("========");
        $client = ClientBuilder::create()->setHosts(['172.17.0.6:9200'])->build();
        //    执行 只用执行一次即可
        //        $client->index($records);
        $client->indices()->create($params);
    }

}
